# Copyright 1998-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file was written by Elena Zannoni (ezannoni@cygnus.com)

# this file tests maintenance commands and help on those.

# source file used is break.c


#maintenance check-psymtabs -- Check consistency of psymtabs vs symtabs
#maintenance check-symtabs -- Check consistency of symtabs
#maintenance expand-symtabs -- Expand symtabs matching a file regexp
#maintenance set -- Set GDB internal variables used by the GDB maintainer
#maintenance show -- Show GDB internal variables used by the GDB maintainer
#maintenance dump-me -- Get fatal error; make debugger dump its core
#maintenance print -- Maintenance command for printing GDB internal state
#maintenance info -- Commands for showing internal info about the program being debugged
#maintenance internal-error -- Give GDB an internal error.
#
#maintenance print dummy-frames -- Print the dummy frame stack
#maintenance print statistics -- Print statistics about internal gdb state
#maintenance print objfiles -- Print dump of current object file definitions
#maintenance print psymbols -- Print dump of current partial symbol definitions
#maintenance print msymbols -- Print dump of current minimal symbol definitions
#maintenance print symbols -- Print dump of current symbol definitions
#maintenance print type -- Print a type chain for a given symbol
#maintenance print unwind -- Print unwind table entry at given address
#
#
#maintenance info sections -- List the BFD sections of the exec and core files
#maintenance info breakpoints -- Status of all breakpoints
#



standard_testfile break.c break1.c

if {[prepare_for_testing $testfile.exp $testfile \
	 [list $srcfile $srcfile2] {debug nowarnings}]} {
    untested $testfile.exp
    return -1
}

# The commands we test here produce many lines of output; disable "press
# <return> to continue" prompts.
gdb_test_no_output "set height 0"

# Tests that require that no program is running

gdb_file_cmd ${binfile}

# Test for a regression where this command would internal-error if the
# program wasn't running.
gdb_test "maint print registers" "Name.*Nr.*Rel.*Offset.*Size.*Type.*"

# Test "mt expand-symtabs" here as it's easier to verify before we
# run the program.
gdb_test_no_output "mt set per on" "mt set per on for expand-symtabs"
gdb_test_multiple "mt expand-symtabs $subdir/break\[.\]c$" \
    "mt expand-symtabs" {
	-re "#compunits: (1|2) \\(\[+\](0|1|2)\\),.*$gdb_prompt $" {
	    # This should expand at most two primary symtabs.
	    # "Normally" it will not expand any, because the symtab
	    # holding "main" will already have been expanded, but if the
	    # file is compiled with -fdebug-types-section then a second primary
	    # symtab for break.c will be created for any types.
	    pass "mt expand-symtabs"
	}
    }
gdb_test "mt set per off" ".*" "mt set per off for expand-symtabs"

# Tests that can or should be done with a running program

gdb_load ${binfile}

if ![runto_main] then {
        perror "tests suppressed"
}

# If we're using .gdb_index there will be no psymtabs.
set have_gdb_index 0
gdb_test_multiple "maint info sections .gdb_index" "check for .gdb_index" {
    -re ": .gdb_index.*$gdb_prompt $" {
	set have_gdb_index 1
    }
    -re ".*$gdb_prompt $" {
	;# Nothing to do, present to avoid a FAIL.
    }
}

#
# this command does not produce any output
# unless there is some problem with the symtabs and psymtabs
# so that branch will really never be covered in this tests here!!
#

# guo: on linux this command output is huge.  for some reason splitting up
# the regexp checks works.
#
send_gdb "maint check-psymtabs\n"
gdb_expect  {
    -re "^maint check-psymtabs" {
	gdb_expect {
	    -re "$gdb_prompt $" {
		pass "maint check-psymtabs"
	    }
	    timeout { fail "(timeout) maint check-psymtabs" }
	}
    }
    -re ".*$gdb_prompt $"     { fail "maint check-psymtabs" }
    timeout         { fail "(timeout) maint check-psymtabs" }
}

# This command does not produce any output unless there is some problem
# with the symtabs, so that branch will really never be covered in the
# tests here!!
gdb_test_no_output "maint check-symtabs"

# Test per-command stats.
gdb_test_no_output "maint set per-command on"
gdb_test "pwd" \
    "Command execution time: \[0-9.\]+ \\(cpu\\), \[0-9.\]+ \\(wall\\)\[\r\n\]+Space used: $decimal \\(\\+$decimal for this command\\)\[\r\n\]+#symtabs: $decimal \\(\\+$decimal\\), #compunits: $decimal \\(\\+$decimal\\), #blocks: $decimal \\(\\+$decimal\\)"
gdb_test_no_output "maint set per-command off"

# The timeout value is raised, because printing all the symbols and
# statistical information about Cygwin and Windows libraries takes a lot
# of time.
if [istarget "*-*-cygwin*"] {
	set oldtimeout $timeout
	set timeout [expr $timeout + 500]
}

send_gdb "maint print statistics\n"
gdb_expect  {
    -re "Statistics for\[^\n\r\]*maint\[^\n\r\]*:\r\n  Number of \"minimal\" symbols read: $decimal\r\n(  Number of \"partial\" symbols read: $decimal\r\n)?  Number of \"full\" symbols read: $decimal\r\n  Number of \"types\" defined: $decimal\r\n(  Number of psym tables \\(not yet expanded\\): $decimal\r\n)?(  Number of read CUs: $decimal\r\n  Number of unread CUs: $decimal\r\n)?  Number of symbol tables: $decimal\r\n  Number of symbol tables with line tables: $decimal\r\n  Number of symbol tables with blockvectors: $decimal\r\n  Total memory used for objfile obstack: $decimal\r\n  Total memory used for BFD obstack: $decimal\r\n  Total memory used for psymbol cache: $decimal\r\n  Total memory used for macro cache: $decimal\r\n  Total memory used for file name cache: $decimal\r\n" {
	gdb_expect {
	    -re "$gdb_prompt $" {
		pass "maint print statistics"
	    }
	    timeout { fail "(timeout) maint print statistics" }
	}
    }
    -re ".*$gdb_prompt $"     { fail "maint print statistics" }
    timeout         { fail "(timeout) maint print statistics" }
}

# There aren't any ...
gdb_test_no_output "maint print dummy-frames"

send_gdb "maint print objfiles\n"

# To avoid timeouts, we avoid expects with many .* patterns that match
# many lines.  Instead, we keep track of which milestones we've seen
# in the output, and stop when we've seen all of them.

set header 0
set psymtabs 0
set symtabs 0
set keep_looking 1

while {$keep_looking} {
    gdb_expect  {

	-re "\r\n" {
	    set output $expect_out(buffer)
	    if {[regexp ".*Object file.*maint($EXEEXT)?:  Objfile at ${hex}" $output]} {
		set header 1
	    }
	    if {[regexp ".*Psymtabs:\[\r\t \]+\n" $output]} {
		set psymtabs 1
	    }
	    if {[regexp ".*Symtabs:\[\r\t \]+\n" $output]} {
		set symtabs 1
	    }
	}

	-re ".*$gdb_prompt $" { 
	    set keep_looking 0
	}
	timeout { 
	    fail "(timeout) maint print objfiles" 
	    set keep_looking 0
	}
    }
}

proc maint_pass_if {val name} {
    if $val { pass $name } else { fail $name }
}

maint_pass_if $header   "maint print objfiles: header"
if { ! $have_gdb_index } {
    maint_pass_if $psymtabs "maint print objfiles: psymtabs"
}
maint_pass_if $symtabs  "maint print objfiles: symtabs"

gdb_test "maint print psymbols" \
    "print-psymbols takes an output file name and optional symbol file name" \
    "maint print psymbols w/o args"

if { ! $have_gdb_index } {
    set psymbols_output [standard_output_file psymbols_output]
    send_gdb "maint print psymbols $psymbols_output ${srcdir}/${subdir}/${srcfile}\n"
    gdb_expect  {
	-re "^maint print psymbols $psymbols_output \[^\n\]*\r\n$gdb_prompt $" {
	    send_gdb "shell ls $psymbols_output\n"
	    gdb_expect {
		-re "$psymbols_output\r\n$gdb_prompt $" {
		    # We want this grep to be as specific as possible,
		    # so it's less likely to match symbol file names in
		    # psymbols_output.  Yes, this actually happened;
		    # poor expect got tons of output, and timed out
		    # trying to match it.   --- Jim Blandy <jimb@cygnus.com>
		    send_gdb "shell grep 'main.*function' $psymbols_output\n"
		    gdb_expect {
			-re ".main., function, $hex.*$gdb_prompt $" {
			    pass "maint print psymbols 1"
			}
			-re ".*main.  .., function, $hex.*$gdb_prompt $" {
			    pass "maint print psymbols 2"
			}
			-re ".*$gdb_prompt $" { fail "maint print psymbols" }
			timeout     { fail "(timeout) maint print psymbols" }
		    }
		    gdb_test "shell rm -f $psymbols_output" ".*" \
			"shell rm -f psymbols_output"
		}
		-re ".*$gdb_prompt $"       { fail "maint print psymbols" }
		timeout           { fail "(timeout) maint print psymbols" }
	    }
	}
	-re ".*$gdb_prompt $"       { fail "maint print psymbols" }
	timeout           { fail "(timeout) maint print psymbols" }
    }
}

gdb_test "maint print msymbols" \
    "print-msymbols takes an output file name and optional symbol file name" \
    "maint print msymbols w/o args"


set msymbols_output [standard_output_file msymbols_output]
send_gdb "maint print msymbols $msymbols_output ${binfile}\n"
gdb_expect  {
    -re "^maint print msymbols $msymbols_output \[^\n\]*\r\n$gdb_prompt $" {
	send_gdb "shell ls $msymbols_output\n"
	gdb_expect {
	    -re "$msymbols_output\r\n$gdb_prompt $" {
		send_gdb "shell grep factorial $msymbols_output\n"
		gdb_expect {
		    -re "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*$gdb_prompt $" {
			pass "maint print msymbols"
		    }
		    -re ".*$gdb_prompt $"     { fail "maint print msymbols" }
		    timeout         { fail "(timeout) maint print msymbols" }
		}
		gdb_test "shell rm -f $msymbols_output" ".*" \
		    "shell rm -f msymbols_output"
	    }
	    -re ".*$gdb_prompt $"     { fail "maint print msymbols" }
	    timeout         { fail "(timeout) maint print msymbols" }
	}
    }
    -re ".*$gdb_prompt $"     { fail "maint print msymbols" }
    timeout         { fail "(timeout) maint print msymbols" }
}

# Check that maint print msymbols allows relative pathnames
set mydir [pwd]
gdb_test "cd [standard_output_file {}]" \
    "Working directory .*\..*" \
    "cd to objdir"

gdb_test_multiple "maint print msymbols msymbols_output2 ${testfile}" "maint print msymbols" {
    -re "^maint print msymbols msymbols_output2 \[^\n\]*\r\n$gdb_prompt $" {
    	gdb_test_multiple "shell ls msymbols_output2" "maint print msymbols" {
	    -re "msymbols_output2\r\n$gdb_prompt $" {
	    	gdb_test_multiple "shell grep factorial msymbols_output2" "maint print msymbols" {
		    -re "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*$gdb_prompt $" {
		    	pass "maint print msymbols"
		    }
		    -re ".*$gdb_prompt $" {
		        fail "maint print msymbols"
		    }
		    timeout {
		        fail "(timeout) maint print msymbols"
		    }
		}
		gdb_test "shell rm -f msymbols_output2" ".*" \
		    "shell rm -f msymbols_output2"
	    }
	    -re ".*$gdb_prompt $" {
		fail "maint print msymbols"
	    }
	    timeout {
	    	fail "(timeout) maint print msymbols"
	    }
	}
    }
    -re ".*$gdb_prompt $" {
	fail "maint print msymbols"
    }
    timeout {
	fail "(timeout) maint print msymbols"
    }
}
gdb_test "cd ${mydir}" \
    "Working directory [string_to_regexp ${mydir}]\..*" \
    "cd to mydir"

gdb_test "maint print symbols" \
    "Arguments missing: an output file name and an optional symbol file name" \
    "maint print symbols w/o args"


# Request symbols for one particular source file so that we don't try to
# dump the symbol information for the entire C library - over 500MB nowadays
# for GNU libc.

set symbols_output [standard_output_file symbols_output]
send_gdb "maint print symbols $symbols_output ${srcdir}/${subdir}/${srcfile}\n"
gdb_expect  {
    -re "^maint print symbols $symbols_output \[^\n\]*\r\n$gdb_prompt $" {
	send_gdb "shell ls $symbols_output\n"
	gdb_expect {
	    -re "$symbols_output\r\n$gdb_prompt $" {
		# See comments for `maint print psymbols'.
		send_gdb "shell grep 'main(.*block' $symbols_output\n"
		gdb_expect {
		    -re "int main\\(int, char \\*\\*, char \\*\\*\\); block.*$gdb_prompt $" {
			pass "maint print symbols"
		    }
		    -re ".*$gdb_prompt $"     { fail "maint print symbols" }
		    timeout         { fail "(timeout) maint print symbols" }
		}
		gdb_test "shell rm -f $symbols_output" ".*" \
		    "shell rm -f symbols_output"
	    }
	    -re ".*$gdb_prompt $"     { fail "maint print symbols" }
	    timeout         { fail "(timeout) maint print symbols" }
	}
    }
    -re ".*$gdb_prompt $"     { fail "maint print symbols" }
    timeout         { fail "(timeout) maint print symbols" }
}

set msg "maint print type"
gdb_test_multiple "maint print type argc" $msg {
    -re "type node $hex\r\nname .int. \\($hex\\)\r\ntagname .<NULL>. \\($hex\\)\r\ncode $hex \\(TYPE_CODE_INT\\)\r\nlength \[24\]\r\nobjfile $hex\r\ntarget_type $hex\r\npointer_type $hex\r\nreference_type $hex\r\ntype_chain $hex\r\ninstance_flags $hex\r\nflags\r\nnfields 0 $hex\r\n$gdb_prompt $" {
        pass $msg
    }
}

if [istarget "hppa*-*-11*"] {
    setup_xfail hppa*-*-*11* CLLbs14860
    gdb_test_multiple "maint print unwind &main" "maint print unwind" {
        -re ".*unwind_table_entry \\($hex\\):\r\n\tregion_start = $hex <main>\r\n\tregion_end = $hex <main\\+\[0-9\]*>\r\n\tflags = Args_stored Save_RP\r\n\tRegion_description = $hex\r\n\tEntry_FR = $hex\r\n\tEntry_GR = $hex\r\n\tTotal_frame_size = $hex\r\n$gdb_prompt $" {
	    pass "maint print unwind"
	}
        -re ".*unwind_table_entry \\($hex\\):\r\n\tregion_start = $hex <main>\r\n\tregion_end = $hex <main\\+\[0-9\]*>\r\n\tflags = Args_stored Save_RP\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n\tFLD = $hex\r\n$gdb_prompt $" {
	    xfail "maint print unwind"
	}
    }
}

set oldtimeout $timeout
set timeout [expr $timeout + 300]

# It'd be nice to check for every possible section.  However, that's
# problematic, since the relative ordering wanders from release to
# release of the compilers.  Instead, we'll just check for two
# sections which appear to always come out in the same relative
# order.  (If that changes, then we should just check for one
# section.)
#
# And by the way: This testpoint will break for PA64, where a.out's
# are ELF files.

# Standard GNU names.
set text_section ".text"
set data_section ".data"

gdb_test_multiple "maint info sections" "maint info sections" {
    -re "Exec file:\r\n.*maint($EXEEXT)?., file type.*ER_RO.*$gdb_prompt $" {
	# Looks like RealView which uses different section names.
	set text_section ER_RO
	set data_section ER_RW
	pass "maint info sections"
    }
    -re "Exec file:\r\n.*maint($EXEEXT)?., file type.*neardata.*$gdb_prompt $" {
	# c6x doesn't have .data section.  It has .neardata and .fardata section.
	set data_section ".neardata"
	pass "maint info sections"
    }
    -re "Exec file:\r\n.*maint($EXEEXT)?., file type.*$gdb_prompt $" {
	pass "maint info sections"
    }
}

# Test for new option: maint info sections <section name>
# If you don't have a .text section, this will require tweaking.

gdb_test_multiple "maint info sections $text_section" \
    "maint info sections .text" {
	-re ".* \\.bss .*$gdb_prompt $" {
	    fail "maint info sections .text"
	}
	-re ".* $data_section .*$gdb_prompt $" {
	    fail "maint info sections .text"
	}
	-re ".* $text_section .*$gdb_prompt $" {
	    pass "maint info sections .text"
	}
    }

# Test for new option: CODE section flag
# If your data section is tagged CODE, xfail this test.

gdb_test_multiple "maint info sections CODE" "maint info sections CODE" {
    -re ".* $data_section .*$gdb_prompt $" { fail "maint info sections CODE" }
    -re ".* $text_section .*$gdb_prompt $" { pass "maint info sections CODE" }
}

# Test for new option: DATA section flag
# If your text section is tagged DATA, xfail this test.
#
# The "maint info sections DATA" test is marked for XFAIL on Windows,
# because Windows has text sections marked DATA.
setup_xfail "*-*-*cygwin*"
setup_xfail "*-*-*mingw*"

gdb_test_multiple "maint info sections DATA" "maint info sections DATA" {
    -re ".* $text_section .*$gdb_prompt $" { fail "maint info sections DATA" }
    -re ".* $data_section .*$gdb_prompt $" { pass "maint info sections DATA" }
    -re ".* .rodata .*$gdb_prompt $" { pass "maint info sections DATA" }
}

set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]

gdb_test_multiple "maint info breakpoints" "maint info breakpoints" {
    -re "Num\[ \t\]+Type\[ \t\]+Disp\[ \t\]+Enb\[ \t\]+Address\[ \t\]+What\r\n1\[ \t\]+breakpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex\[ \t\]+in main at.*break.c:$bp_location6 inf 1\r\n\[ \t\]+breakpoint already hit 1 time\r\n.*$gdb_prompt $" {
	pass "maint info breakpoints"
    }
    -re "Num\[ \t\]+Type\[ \t\]+Disp\[ \t\]+Enb\[ \t\]+Address\[ \t\]+What\r\n1\[ \t\]+breakpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex in main at.*break.c:$bp_location6 sspace 1\r\n\[ \t\]+breakpoint already hit 1 time\r\n-1\[ \t\]+shlib events\[ \t\]+keep\[ \t\]+y\[ \t\]+$hex.*breakpoint already hit.*$gdb_prompt $" {
	pass "maint info breakpoints (with shlib events)"
    }
}

gdb_test "maint print" \
    "\"maintenance print\" must be followed by the name of a print command\\.\r\nList.*unambiguous\\..*" \
    "maint print w/o args" 

gdb_test "maint info" \
    "\"maintenance info\" must be followed by the name of an info command\\.\r\nList.*unambiguous\\..*" \
    "maint info w/o args"

gdb_test "maint" \
    "\"maintenance\" must be followed by the name of a maintenance command\\.\r\nList.*unambiguous\\..*" \
    "maint w/o args"

set timeout $oldtimeout

#============test help on maint commands

gdb_test "help maint" \
    "Commands for use by GDB maintainers\\..*Includes commands to dump specific internal GDB structures in.*a human readable form, to cause GDB to deliberately dump core, etc\\..*List of maintenance subcommands:.*maintenance info.*maintenance internal-error.*maintenance print.*maintenance set.*maintenance show.*Type.*help maintenance.*followed by maintenance subcommand name for full documentation\\..*Command name abbreviations are allowed if unambiguous\\..*" 

gdb_test "help maint info" \
    "Commands for showing internal info about the program being debugged.*unambiguous\\..*"

test_prefix_command_help {"maint print" "maintenance print"} {
    "Maintenance command for printing GDB internal state\\.\[\r\n\]+"
}

test_prefix_command_help {"maint" "maintenance"} {
    "Commands for use by GDB maintainers\\.\[\r\n\]+"
    "Includes commands to dump specific internal GDB structures in\[\r\n\]+"
    "a human readable form, to cause GDB to deliberately dump core, etc\\.\[\r\n\]+"
}

#set oldtimeout $timeout
#set timeout [expr $timeout + 300]

gdb_test_multiple "maint dump-me" "maint dump-me" {
    -re "Should GDB dump core.*\\(y or n\\) $" {
	gdb_test "n" ".*" "maint dump-me"
    }
    -re "Undefined maintenance command: .*$gdb_prompt $" {
	# Command 'maint dump-me' is registered on non-win32 host.
	unsupported "maint dump-me"
    }
}

send_gdb "maint internal-error\n"
gdb_expect {
    -re "A problem internal to GDB has been detected" {
	pass "maint internal-error"
	if [gdb_internal_error_resync] {
	    pass "internal-error resync"
	} else {
	    fail "internal-error resync"
	}
    }
    -re ".*$gdb_prompt $" {
	fail "maint internal-error"
	untested "internal-error resync"
    }
    timeout {
	fail "maint internal-error (timeout)"
	untested "internal-error resync"
    }
}

#set timeout $oldtimeout


gdb_exit
return 0
